<template>
    <div class="page_reset every-block-bgcolor">
        <div class="view active ">
            <common-header :title="'修改密码'" :showBack="true"></common-header>

            <div class="pages panel active">
                <div class="login-item">
                    <x-input title="旧密码" v-model="formItem.oldpwd" placeholder="请输入旧密码" :max="13"></x-input>
                </div>
                <div class="login-item">
                    <x-input title="新密码" type="password" v-model="formItem.newpwd" placeholder="请输入新密码"  :max="13"></x-input>
                </div>
                <div class="login-item">
                    <x-input title="确认新密码" type="password" placeholder="请确认密码"  v-model="formItem.againpwd"
                             :max="13"></x-input>
                </div>
                <div class="weui-cell" style="border-bottom: 1px solid #f4f4f4;margin:0 0.6rem;">
                    <div class="weui-cell__hd">
                        <label class="weui-label">图形验证码</label>
                    </div>
                    <div class="weui-cell__bd">
                        <input class="weui-input" v-model="formItem.yzm" type="text" name="yzm" placeholder="请输入验证码"
                               weui-required>
                    </div>
                    <div class="weui-cell__ft" @click="getPic()" style="display: inline-block !important;">
                        <a>
                            <img :src="'data:image/jpeg;base64,' +  VerificationCode" width="70px" height="25px">
                        </a>
                    </div>
                </div>
                    <div  class="btn-next-block" >
                        <button   class="btn-next weui-btn btn-check" @click="changePassWord">修改密码</button>
                    </div>
            </div>
        </div>

    </div>
</template>

<script>
    import login from '../../../services/login'
    import user from '../../../services/user'
    import {XInput} from 'vux'

    export default {
        data() {
            return {
                formItem: {
                    oldpwd: '',
                    newpwd: '',
                    againpwd: '',
                    yzm: '',
                },
                VerificationCode: '',
            }
        },
        name:'resetPwd',
        components: {XInput},
        created() {
            this.getPic()
        },
        methods: {

            //获取验证码
            getPic() {
                let _param = this.$tools.invokeUAF('user/getpicvericode', {
                    'imagewidth': 100,
                    'imageheight': 25,
                    'codelength': 4
                })
                login.getpicvericode(_param).then(res => {
                    let data = res.data.data;
                    this.VerificationCode = data.image;
                    this.$tools.setMessage('pictoken', data.token)
                    // this.$tools.setMessage('regioncode', '1562228444372')
                })
            },

            // 修改密码
            changePassWord() {

                let param = {
                    oldpwd: '请输入旧密码',
                    newpwd: '请输入新密码',
                    againpwd: '请确认密码',
                    check: '两次输入密码不一致，请重新输入',
                    yzm: '验证码不能为空',
                }

                for (let i in param) {
                    if (i !== 'check') {
                        if (!this.formItem[i]) {
                            this.$vux.toast.text(param[i]);
                            return;
                        }
                    } else {
                        if (this.newpwd !== this.oldpwd) {
                            this.$vux.toast.text(param[i])
                        }
                    }

                }

                let _params = {
                    "uat": this.uat,
                    "oldpwd": this.$tools.saltpwd(this.formItem.oldpwd),
                    "newpwd": this.$tools.saltpwd(this.formItem.newpwd),
                    "picvericode": this.formItem.yzm,
                    "pictoken": this.$tools.getMessage('pictoken'),
                }

                let params = this.$tools.invokeUAF('user/modifypwd', _params)
                user.modifypwd(params).then(res => {
                    let code = res.data.code;
                    if (code === '0') {
                        this.$vux.toast.show({
                            type: 'text',
                            text: res.data.data
                        });
                        this.$tools.goPage('userInfo')
                    }
                })

            },
        }
    }
</script>

<style lang="less" rel="stylesheet/less" type="text/less">
    .page_reset {
        .login-item {
            border-bottom: 1px solid #f4f4f4;
            margin: 0 .6rem;
        }

        .weui-cell {
            font-size: 17px !important;
        }

        .weui-input {
            border: none;
        }

        .weui-cell__hd {
            background: transparent !important;
            padding-right: 0.35em !important;
        }

        .weui-label {
            width: 110px !important;
        }

    }
</style>
